import _clonedeep from 'lodash.clonedeep'
import _capitalize from 'lodash.capitalize'
import * as api from '@/const/api'

export const state = () => ({
  isPC: false,
  isApp: false,
  lang: 'zh',
  solutionData: [],
  productData: [],
  menus: [
    {
      id: '99991',
      en: 'Solutions',
      zh: '解决方案',
      path: '/solution-list',
      children: [],
    },
    {
      id: '99992',
      en: 'Products',
      zh: '产品列表',
      path: '/product',
      children: [],
    },
    { id: '99993', en: 'Service', zh: '服务支持', path: '/service' },
    {
      id: '99994',
      en: 'Career',
      zh: '工作机会',
      path: '/work/reason',
      children: [
        {
          id: '4-1',
          en: 'Reasons forchoosing soten',
          zh: '选择硕腾的原因',
          path: '/work/reason',
        },
        {
          id: '4-2',
          en: 'Team story',
          zh: '团队故事',
          path: '/work/team',
        },
        {
          id: '4-3',
          en: 'Team activity',
          zh: '团建活动',
          path: '/work/activity',
        },
        {
          id: '4-4',
          en: 'Personnel training',
          zh: '人才培养',
          path: '/work/personnel',
        },
        {
          id: '4-5',
          en: 'Trade union',
          zh: '工会工作',
          path: '/work/trade-union',
        },
        {
          id: '4-6',
          en: 'Join us',
          zh: '加入我们',
          path: '/work/join-us',
        },
      ],
    },
    {
      id: '99995',
      en: 'About Us',
      zh: '关于硕腾',
      path: '/about-us',
      // children: [
      // {
      //   id: '5-1',
      //   en: 'About us',
      //   zh: '关于硕腾',
      //   path: '/about-us',
      // },
      // {
      //   id: '5-2',
      //   en: 'News center',
      //   zh: '新闻中心',
      //   path: '/news-center',
      // },
      // ],
    },
    { id: '99996', en: 'Contact Us', zh: '联系硕腾', path: '/contact-us' },
  ],
  token: '',
  userInfo: '',
  isZh: true,
})

export const getters = {
  // 语言首字母大写
  capitalizeLang(state) {
    return _capitalize(state.lang)
  },
}

export const mutations = {
  update(state, payload) {
    Object.keys(payload).forEach((k) => {
      state[k] = payload[k]
    })
  },
}

export const actions = {
  // 拉取菜单信息
  async fetchMenuInfo({ commit, state }) {
    try {
      const allAjax = []
      allAjax.push(
        this.$axios.$post(api.classificationPage, {
          type: 1,
        })
      )
      allAjax.push(
        this.$axios.$post(api.classificationPage, {
          type: 2,
        })
      )

      // 获取基本数据
      const res = await Promise.all(allAjax)

      // 解决方案数据
      const solutionData = res[0]?.data
      // 产品管理数据
      const productData = res[1]?.data

      const solutionIds = solutionData.map(({ id }) => id)
      const productIds = productData.map(({ id }) => id)
      // 获取分类下数据
      const { data } = await this.$axios.$post(api.solutionPage, {
        classificationIds: [...solutionIds, ...productIds],
        pageSize: 1000,
        pageNo: 1,
      })

      // 分类下对应数据
      const classificationMap = {}
      data.list.forEach((item) => {
        const { classificationId } = item
        if (!classificationMap[classificationId]) {
          classificationMap[classificationId] = []
        }
        item.path = `/solution?id=${item.url}&type=${item.classificationType}`
        item.zh = item.titleZh
        item.en = item.titleEn
        classificationMap[classificationId].push(item)
      })

      const cloneMenus = _clonedeep(state.menus)
      // 重新组装menu
      cloneMenus[0].children = solutionData.map((item, index) => {
        return {
          zIndex: `1-${index}`,
          ...item,
          id: `1-${index}`,
          en: item.nameEn,
          zh: item.nameZh,
          children: classificationMap[item.id] || [],
        }
      })

      cloneMenus[1].children = productData.map((item, index) => {
        return {
          zIndex: `2-${index}`,
          ...item,
          id: `2-${index}`,
          en: item.nameEn,
          zh: item.nameZh,
          children: classificationMap[item.id] || [],
        }
      })

      commit('update', {
        menus: cloneMenus,
        solutionData,
        productData,
      })
      return true
    } catch (e) {
      return false
    }
  },
  // 通过ip获取国外还是国外
  async fetchZoneCn({ commit }) {
    try {
      const { data } = await this.$axios.$get(api.zoneCn)
      commit('update', {
        lang: data ? 'zh' : 'en',
        isZh: data,
      })
    } catch (err) {
      return false
    }
  },
}
